function tabInit(){
	$("#tabs .tab").not(".newtab").click(function(e){
		switchTab($(this));
	})
}

/****/
function SelectTab(tableId,tableTitle,autoSwitchTab){
	var tab = $("#tabs .tab[table="+tableId+"]");
	if(tab.length == 0)
		return createTab(tableId,tableTitle,autoSwitchTab);
	else
		switchTab(tab);
}

function createTab(tableId,tableTitle,autoSwitchTab){
	var lastTab = $("#tabs .tab").not(".newtab").last();
	var table = getObjectFromSharedData(SharedData.Table,tableId);
	if(table==null) table = {id:tableId,tableTitle:tableTitle};
	var tab = $("<div class=\"tab normaltab\" table=\""+tableId+"\" tabindex=\"1\"><span class='label'>"+table.tableTitle+"</span><span class='closetab'></span></div>");
	lastTab.after(tab);
	
	reindexTab();
	tab.click(function(e){
		switchTab($(this));
	});
	tab.find('.closetab').click(function(){
		var tableId = $(this).parents('.tab:first').attr("table");
		closeTabTable(tableId);
	});
	delete window._tmpindex;
	var obj = {table:table};
	var res = getObjectFromDataIndex(SharedData.POSReservationTable,"tableId",tableId);
	if(res!=null) {obj.OwnerTable = {}; obj.OwnerTable.Name = res.Name; obj.OwnerTable.resId = res.id;}
	POS.push(obj);
	if(autoSwitchTab==null||autoSwitchTab)switchTab(tab);
	arrangeTab();
	
	window.focus();
	return obj;
}

function reindexTab(){
	window._tmpindex = 0;
	$("#tabs .tab").each(function(){
		$(this).attr("tabindex",window._tmpindex++);
		$(this).removeClass("active");
	});
}

function arrangeTab(){
	var width = $("#tabs").attr("tabwidth");
	if($(".tab").size()*width > $("[name=Block]").width()) width = $("[name=Block]").width()/$(".tab").size();
	$("#tabs .tab").not(".newtab").css("width",width);
	$("#tabs .tab").each(function(){
		var width = $("#tabs").attr("tabwidth");
		if($(".tab").size()*width > $("[name=Block]").width()) width = $("[name=Block]").width()/$(".tab").size();
		var margin = $(this).attr("tabindex") * (width - 3);
		$(this).css("margin-left",margin);
		var max = $("[tabindex][table]:last").attr("tabindex");
		if(!$(this).hasClass("active"))
			$(this).css("z-index",100 - (parseInt($(this).attr("tabindex"))));
		else
			$(this).css("z-index",101);
	});
}

function switchTab(dom,attrlist){
	if(dom==null) dom = $("#tabs .tab.active");
	$("#tabs .tab").removeClass("active");
	dom.addClass("active");
	arrangeTab();
	displayTabSelector(dom,attrlist);
	
	$("[id='BlankDialog'][type=RightClick]").dialog("close");
	
	function displayTabSelector(dom,attrlist){
		var Tabs = getObjectFromDataIndexArray(POS,["table","id"],dom.attr("table"));
		if(attrlist!=null) for(var k in attrlist) Tabs[k] = attrlist[k];
		$("div[name=AllTable]").css("display","none");
		$("div[for=pageload]").css("display","none");
		$("div[for=pageload]").children().css("display","none");
		//Table Page
		if(dom.attr("table")=="All"){
			$("div[name=AllTable]").css("display","");
//			$('.detailicon').removeClass("rightItem");
//			$('.detailicon').addClass("rightItem");
		}
		//Generate Order Page
		else if(Tabs.pagetitle == "orderlist"){
			delete Tabs.orderItemList;
			waitingDialog();
			loadDynamicJSP($("div[for=pageload]"),"/POS/GenerateOrder.jsp",waitingDialog,function(resp,obj){
				$("div[for=pageload]").css("display","");
				$("div[for=pageload]").children("[file='/POS/GenerateOrder']").css("display","");
				LoadConfirmOrderpage();
			},{dom:$(this)});
			checktoShowUpdate();
		}
		else if(Tabs.pagetitle == "payment"){
			loadDynamicJSP($("div[for=pageload]"),"/POS/POSPayment.jsp",waitingDialog,function(resp,obj){
				$("div[for=pageload]").css("display","");
				$("div[for=pageload]").children("[file='/POS/POSPayment']").css("display","");
				closeWaitingDialog();
				fillPageDetail();
			},{dom:$(this)});
		}
		//Order Item Page
		else{
			waitingDialog();
			loadDynamicJSP($("div[for=pageload]"),"/POS/OrderItem.jsp",waitingDialog,function(resp,obj){
				closeWaitingDialog();
				$("div[for=pageload]").css("display","");
				$("div[for=pageload]").children("[file='/POS/OrderItem']").css("display","");
				displayOrderItem();
				autoResizeElement();
			});
		}
	}
}

/****/
function closeTabTable(tableId){
	if(tableId==null) tableId = $("#tabs .tab.active").attr("table");
	var tab = $("#tabs .tab[table="+tableId+"]");
	for(var _i=0;_i<POS.length;_i++)
		if(POS[_i].table.id == tableId){
			POS.splice(POS.indexOf(getTableData(tableId)),1);
			break;
		}
	tab.remove();
	var active = $("#tabs .tab.active");
	reindexTab();
	arrangeTab();
	if(active.length>0) switchTab(active);
	else{
		if(POS[_i] != null) 
			SelectTab(POS[_i].table.id);
		else
			if(POS.length>0)
				SelectTab(POS[POS.length-1].table.id);
			else
				SelectTab("All");
	}
}

function autoResizeElement(){
	$(".orderlistbody").css("height",$(window).height()-288);
	$(".categoryleftitem").css("height",$(window).height()-301);
	$("[name=show_positem]").css("height",$(window).height()-343);
	scrollCategory();
}

function scrollCategory(){
	$(".categoryleftitem").scroll(function(e){
		var activeItem = $(".categoryleftitem [rifid][isactive]");
		if(activeItem.length==0) return;
		var width = "width:"+activeItem.parents("table:first").width()+"px;";
		if(activeItem.offset().top <= $(".categoryleftitem").offset().top){
			if($(".categoryleftitem .fixScrollItem").length==0){
				var item = activeItem.clone();
				item.removeAttr("isactive");
				item.removeAttr("onclick");
				$(".categoryleftitem").prepend("<div class='fixScrollItem' style='"+width+"'><table><tr>"+item.wrap('<div/>').parent().html()+"</tr></table></div>");
			}
		}
		else if(activeItem.offset().top+activeItem.height() >= $(".categoryleftitem").offset().top+$(".categoryleftitem").height()){
			if($(".categoryleftitem .fixScrollItem").length==0){
				var item = activeItem.clone();
				var margintop = "margin-top:" + ($(".categoryleftitem").height() - activeItem.height()) + "px;";
				item.removeAttr("isactive");
				item.removeAttr("onclick");
				$(".categoryleftitem").prepend("<div class='fixScrollItem' style='"+width+margintop+"'><table><tr>"+item.wrap('<div/>').parent().html()+"</tr></table></div>");
			}
		}
		else
			$(".categoryleftitem .fixScrollItem").remove();
	});
}

function checktoShowUpdate(){
	var Tabs = getCurrentTable();
	var orderId= Tabs.activeOrderId;
	$('.genOrder_Orderdetail_head').children('[name=UpdateOrderoption]:visible').remove();
	$('.genOrder_Orderdetail_head').children().show();
	$('[name=genOrder_btnpayment]').show();
	$('[refid='+orderId+']').removeClass("whenUpdateOrder");
	$($('.genOrder_Orderdetail_btnbtm').children()[2]).show();
	$('.genOrder_Orderdetail_btnbtm').children().first().show();
	$('[name=UpdateOrderoption]').remove();
	$('[name=POS_updateOrderbtn]').show();
	var headText='<span>Order</span>';
	$('[name=OrderNumber]').html('');
	$('[name=OrderNumber]').append(headText);
}